package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/cart/")
public class CartController {
    @Autowired
    private CartService cartService;

    //由username获取购物车
    @GetMapping("cartitems")
    @ResponseBody
    public CommonResponse<List<CartVO>> getCartByUsername(HttpSession session){
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();

        List<CartVO> cartVOList = cartService.getCartItemsByUsername(username).getData();

        CommonResponse<List<CartVO>> response = cartService.getCartItemsByUsername(username);
        session.setAttribute("cartVOList", cartVOList);
        return response;
    }

    //获取总价
    @GetMapping("cartTotal")
    @ResponseBody
    public CommonResponse<BigDecimal> getCartTotal(HttpSession session){
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();

        List<CartVO> cartVOList = cartService.getCartItemsByUsername(username).getData();
        BigDecimal total= BigDecimal.valueOf(0);
        for(int i=0;i<cartVOList.size();i++){
            total = total.add(cartVOList.get(i).getTotalprice());
        }
        System.out.println(total+"=============total==============");

        return CommonResponse.createForSuccess(total);
    }

    ////由商品界面直接添加到购物车
    @PostMapping("cartitems/{itemId}")
    @ResponseBody
    public CommonResponse<List<CartVO>> addCartItem(HttpSession session, @PathVariable("itemId") String itemId)
    {
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();
        CommonResponse<List<CartVO>> response = cartService.addCartItem(username,itemId);
        List<CartVO> cartVOList = response.getData();
        session.setAttribute("cartVOList", cartVOList);
        return response;
    }

    //在购物车界面修改数量
    @PostMapping("cartitems/{itemId}/quantity/{quantity}")
    @ResponseBody
    public CommonResponse<List<CartVO>> updateCartItem(HttpSession session,@PathVariable("itemId") String itemId, @PathVariable("quantity") int quantity)
    {
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();
        List<CartVO> cartVOList = cartService.addCartItem(username,itemId,quantity).getData();
        CommonResponse<List<CartVO>> response = cartService.addCartItem(username,itemId,quantity);
        session.setAttribute("cartVOList", cartVOList);
        return response;
    }

    //删除购物车中某个商品
    @DeleteMapping("cartitems/{itemId}")
    @ResponseBody
    public CommonResponse<List<CartVO>> removeCartItem(HttpSession session,@PathVariable("itemId") String itemId, HttpServletRequest request)
    {
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();
        List<CartVO> cartVOList = cartService.deleteCartItem(username,itemId).getData();
        CommonResponse<List<CartVO>> response = cartService.deleteCartItem(username,itemId);
        session.setAttribute("cartVOList", cartVOList);
        return response;
    }

    //清空购物车
    @DeleteMapping("cartitems")
    @ResponseBody
    public CommonResponse<List<CartVO>> clear(HttpSession session)
    {
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        String username = accountVO.getUsername();
        List<CartVO> cartVOList = cartService.clear(username).getData();
        CommonResponse<List<CartVO>> response = cartService.clear(username);
        session.setAttribute("cartVOList", cartVOList);
        return response;
    }

}
